package com.atguigu.eud_teacher.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eud_teacher.entity.CourseEntity;
import com.atguigu.eud_teacher.service.CourseService;
import com.atguigu.eud_teacher.vo.CoursePublishVo;
import com.atguigu.eud_teacher.vo.CourseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


/**
 * 课程
 *
 * @author wuyao
 * @email 13918471632@163.com
 * @date 2021-03-23 12:12:26
 */
@RestController
@RequestMapping("/vue-admin-template/course")
@Api(tags = "课程基本信息管理接口API")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //保存下一步，添加课程基本信息，edu_course,edu_course_description
    @ApiOperation(value = "添加课程基本信息", notes = "注意描述在course_description中")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseEntity courseEntity) {
        //课程基本信息插入，跳转到章节，章节里面需要course_id
        String courseId = courseService.insertCourseAndDescription(courseEntity);
        return R.ok().data("courseId", courseId);
    }

    //根据courseid获取一组数据
    @ApiOperation(value = "根据id获取course基本信息")
    @GetMapping("/getCourseInfoById/{id}")
    public R getCourseInfoById(@PathVariable String id) {
        CourseEntity courseEntity = courseService.selectById(id);
        return R.ok().data("courseEntity", courseEntity);
    }

    //修改course基本信息
    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("/updateCourseInfo")
    public R updateCoursrInfo(@RequestBody CourseEntity courseEntity) {
        courseService.updateCourseInfo(courseEntity);
        return R.ok();
    }

    //根据courseId获取总发布信息
    @ApiOperation(value = "根据id获取总发布信息")
    @GetMapping("/getFinalResult/{id}")
    public R getFinalResult(@PathVariable String id) {
        CoursePublishVo cursePublishVo = courseService.getFinalResult(id);
        return R.ok().data("cursePublishVo", cursePublishVo);
    }

    //修改总发布信息的状态为发布状态
    //根据课程id发布课程
    @ApiOperation(value = "修改总发布信息")
    @PostMapping("/updateFinalResult/{id}")
    public R updateFinalResult(@PathVariable String id) {
        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setId(id);
        courseEntity.setStatus("Normal");
        courseService.updateById(courseEntity);
        return R.ok();
    }

    //list主页面获取所有课程基本信息
    @ApiOperation(value = "获取所有课程基本信息")
    @PostMapping("/getAllSubjectsInfo/{current}/{limit}")
    public R getAllSubjectsInfo(@PathVariable Long current,
                                @PathVariable Long limit,
                                @RequestBody(required = false) CourseQuery courseQuery) {
        Page<CourseEntity> pageCourse = new Page<CourseEntity>(current, limit);
//        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<CourseEntity>();
//        String title = courseQuery.getTitle();
//        String status = courseQuery.getStatus();
//        if (!StringUtils.isEmpty(title)) {
//            wrapper.like("title", title);
//        }
//        if (!StringUtils.isEmpty(status)) {
//            wrapper.eq("status", status);
//        }
//        wrapper.orderByDesc("gmt_create");
        courseService.page(pageCourse, null);
        //总记录数
        long total = pageCourse.getTotal();
        //显示数据集合，详细数据
        List<CourseEntity> records = pageCourse.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }

    //删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("/deleteCourseByCourseId/{courseId}")
    public R deleteCourseByCourseId(@PathVariable String courseId) {
        courseService.deleteCourse(courseId);
        return R.ok();
    }
}
